<template>
	<view class="">
		<headerinfo></headerinfo>
		<backos></backos>
		<view class="width60">
			<view class="searchPage">
				<!-- <u-navbar v-if="showHeader" title="搜索" :placeholder="true" :autoBack="true"></u-navbar> -->
				<!-- <headertop :inv="5"></headertop> -->
				<view class="content" v-if="listData.length > 0">
					<view class="card" v-for="(item, index) in listData" :key="index"><new-item :isMylife="isMylife?true:false"
							:new="item" :newIndex="index"></new-item></view>
				</view>
				<nodata v-if="listData.length == 0 && loadingOver"></nodata>
				<view class="nomore" v-if="query.lastDocId == '' && listData.length > 0">
					没有更多了 ~
				</view>
			</view>
		</view>
	</view>
	
</template>

<script>
	import newItem from '@/components/new-item.vue';
	export default {
		components: {
			newItem
		},
		data() {
			return {
				loadingOver: false,
				query: {
					lastDocId: '',
				},
				listData: [],
				listOver: false,
				isMylife: '',
				showHeader: true
			};
		},
		onPullDownRefresh() {
			this.query.lastDocId = ''
			this.getData()
		},
		onReachBottom() {
			if (!this.listOver) {
				this.getData();
			}
		},
		onLoad(e) {
			console.log('searchover onLoad e',e)
			this.query = JSON.parse(e.queryData);
			if (e.isMylife) {
				this.isMylife = e.isMylife
				this.getData2();
			} else {
				this.getData();
			}
		},
		methods: {
			initSearch(query) {
				this.query = query
				this.showHeader = false
				this.getData();
			},
			getData2() {
				uni.showLoading({
					title: '加载中...'
				})
				this.loadingOver = false
				this.$api.queryLives(this.query).then(res => {
					console.log(res);
					this.listData = [...this.listData, ...res.list];
					if (res.lastDocId) {
						this.query.lastDocId = res.lastDocId;
					} else {
						this.query.lastDocId = ''
						// this.listData = res.list;
					}

					if (res.list.length < this.query.pageSize) {
						this.listOver = true
					}
					this.loadingOver = true
					uni.hideLoading()
					uni.stopPullDownRefresh();
				});
			},
			getData() {
				uni.showLoading({
					title: '加载中...'
				})
				this.loadingOver = false
				this.$api.queryArticles(this.query).then(res => {
					console.log(res);
					// this.listData = res.list;
					this.listData = [...this.listData, ...res.list];
					if (res.lastDocId) {
						this.query.lastDocId = res.lastDocId;
					} else {
						this.query.lastDocId = ''
					}


					if (res.list.length < this.query.pageSize) {
						this.listOver = true
					}
					this.loadingOver = true
					uni.hideLoading()
					uni.stopPullDownRefresh();
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.searchPage {

		// background-color: #f8f8f8;
		.card {
			margin-top: 20rpx;
		}

		.card:first-child {
			margin-top: 0;
		}

		.content {
			width: 100%;
			margin: 0 auto;
			border-radius: 20rpx;
			overflow: hidden;
			box-shadow: 0 0 3px #ccc;
			cursor: pointer;
		}
	}
</style>